package com.roncoo.eshop.inventory.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.roncoo.eshop.inventory.request.Request;
import com.roncoo.eshop.inventory.request.RequestQueue;

/**
 * Copyright: Copyright (c) 2019 Hu-Cheng
 * 
 * @ClassName: RequestProcessorThreadPool.java
 * @Description: 请求处理线程池：单例
 *
 * @version: v1.0.0
 * @author: hucheng
 * @date: 2019年9月19日 上午10:06:18
 *
 */
public class RequestProcessorThreadPool {

	/**
	 * 初始化线程池：10个
	 */
	private ExecutorService threadPool = Executors.newFixedThreadPool(10);

	/**
	 * 构造方法初始化：线程池、内存队列（将线程池与内存队列整合）
	 */
	public RequestProcessorThreadPool() {

		RequestQueue requestQueue = RequestQueue.getInstance();

		for (int i = 0; i < 10; i++) {
			ArrayBlockingQueue<Request> queue = new ArrayBlockingQueue<Request>(100);
			requestQueue.addQueue(queue);
			threadPool.submit(new RequestProcessorThread(queue));
		}

	}

	/**
	 * Copyright: Copyright (c) 2019 Hu-Cheng
	 * 
	 * @ClassName: RequestProcessorThreadPool.java
	 * @Description: 内部静态类方式实现单例模式
	 *
	 * @version: v1.0.0
	 * @author: hucheng
	 * @date: 2019年9月19日 上午10:07:53
	 *
	 */
	private static class Singleton {
		private static RequestProcessorThreadPool requestProcessorThreadPool;

		static {
			requestProcessorThreadPool = new RequestProcessorThreadPool();
		}

		private static RequestProcessorThreadPool getRequestProcessorThreadPool() {
			return requestProcessorThreadPool;
		}
	}

	/**
	 * jvm机制去保证多线程并发安全，内部内的初始化，一定只会发生一次，不管多少个线程去初始化
	 * 
	 * @return RequestProcessorThreadPool
	 */
	public static RequestProcessorThreadPool getInstance() {
		return Singleton.getRequestProcessorThreadPool();
	}

	/**
	 * 初始化方法封装
	 */
	public static void init() {
		getInstance();
	}

}
